package sort;

import java.lang.management.ManagementFactory;

import javax.swing.SwingUtilities;

import sort.algorithm.QuickSort;
import sort.algorithm.Sorter;
import sort.record.RandomRecordsFactory;
import sort.record.Records;
import sort.record.RecordsFactory;

/**
 * @author masaru-gotou
 *
 */
@Deprecated
@SuppressWarnings("serial")
public class SortMain2 extends SortingViewer {
	Sorter sorter;
	Records records;

	public SortMain2(String title) {
//		super(title);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		SortMain2 main = new SortMain2("Sort Demo");
//		main.go();
//		PropertyConfigurator.configure("log4j.properties");
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				SortMain2 main = new SortMain2("Sort Demo");
//				main.init();
				main.setVisible(true);
			}
		});
	}

	protected void init() {
//		RecordsFactory factory = FixedRecordsFactory.getInstance();
		RecordsFactory factory = RandomRecordsFactory.getInstance();
		
		records = factory.create(20);
		setRecords(records);
		
//		Log log = LogFactory.getLog(getClass());
//		super.writeLog(log, records);
		
//		sorter = new BubbleSort();
//		sorter = new SelectionSort();
//		sorter = new InsertionSort();
//		sorter = new BucketSort(200);
		sorter = new QuickSort();
		sorter.addObserver(this);
		setSortAlgoName(sorter.getClass().getName());
		setTime(-1);
		setValidated(false);
		setSorted(false);
		setStabled(false);
		setMemoryUsage(0);
		
//		printMemoryUsage();
	}

	@Override
	protected void start() {
//		sorter.setRecords(records.copy());
//		Thread sort = new Thread(sorter);
		long start = System.currentTimeMillis();
		long start_used_mem = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
//		sort.run();
		Records sorted = sorter.sort(records.copy());
		long end = System.currentTimeMillis();
		long end_used_mem = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
		setTime(end-start);
		setValidated(CheckUtil.isSame(records, sorted) );
		setSorted(CheckUtil.isSorted(sorted));
		setStabled(CheckUtil.isStabled(records, sorted));
		setMemoryUsage(end_used_mem-start_used_mem);
	}

	@Override
	protected void stop() {
		// TODO
		
	}
}
